﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json;
using Meeting.Public;
using Meeting.Model;
using Meeting.Web.Core;

namespace Meeting.Web.Admin.BaseSet
{
    /// <summary>
    /// EmployeeOperation 的摘要说明
    /// </summary>
    public class VoteOperation : IHttpHandler
    {
        string methodName = "";
        int nPageIndex = 1;
        int nPageSize = 20;
        BLL.Vote bll = new BLL.Vote();

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "application/json;charset=utf-8";
            //context.Response.ContentType = "text/plain";
            methodName = context.Request.Params["method"];
            nPageIndex = CommonFunction.varToIntDef(context.Request.Params["page"], 1);
            nPageSize = CommonFunction.varToIntDef(context.Request.Params["rows"], 15);

            switch (methodName.ToLower())
            {
                case "getvotes":   //获取json数据
                    GetVote(context);
                    break;
                case "getbyid":   //获取json数据                      
                    context.Response.Write(bll.GetBy(CommonFunction.varToIntDef(context.Request.Params["param"], 0)));
                    break;
                case "save":
                    context.Response.Write(SaveData(context));
                    break;
                case "delete":
                    context.Response.Write(DeleteData(context));
                    break;
                case "getitemby":
                    context.Response.Write(bll.GetItemsBy(CommonFunction.varToIntDef(context.Request.Params["param"], 0)));
                    break;

                case "getsubofficejosnforgrid":
                    GetSubOfficeJosnForGrid(context);
                    break;
            }
        }


        private void GetVote(HttpContext context)
        {
            string name = CommonFunction.removeAttackOfSql(CommonFunction.VarToStr(context.Request.Params["Name"]));
            string Begin = CommonFunction.removeAttackOfSql(CommonFunction.VarToStr(context.Request.Params["Begin"]));
            string End = CommonFunction.removeAttackOfSql(CommonFunction.VarToStr(context.Request.Params["End"]));
            string sf = "1=1";
            if (!string.IsNullOrEmpty(name)) sf += " And Theme Like '" + name + "%' ";
            if (!string.IsNullOrEmpty(Begin)) sf += " And StartDate >= '" + Begin + "' ";
            if (!string.IsNullOrEmpty(End)) sf += " And StartDate <= '" + End + "' ";
            context.Response.Write(bll.GetPage(nPageIndex, nPageSize, sf));
        }

        /// <summary>
        /// 保存投票
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        private string SaveData(HttpContext context)
        {
            Meeting.Model.Vote model = new Model.Vote();
            model.Id = CommonFunction.varToIntDef(context.Request.Params["id"], 0);
            model.Theme = CommonFunction.removeAttackOfSql(context.Request.Params["theme"]);
            model.Category = CommonFunction.varToIntDef(context.Request.Params["category"], 0);

            model.StartDate = CommonFunction.varToDateTimeDef(context.Request.Params["start"], DateTime.Now);
            model.EndDate = CommonFunction.varToDateTimeDef(context.Request.Params["end"], DateTime.Now);
            model.EmployeeId = UserInfo.UserID;
            model.EmployeeName = UserInfo.UserName;
            model.EmployeeTitle = UserInfo.Title;
            model.RoleId = UserInfo.RoleId;
            model.IsActive = true;
            model.Comment = "";

            List<Model.VoteItem> itemList = null;
            List<Model.VoteSubOffice> subOffices = new List<VoteSubOffice>();
            string delIds = "";
            try
            {
                delIds = CommonFunction.removeAttackOfSql(context.Request.Params["delids"]);
                delIds = delIds.Replace("，", ",");
                string items = CommonFunction.removeAttackOfSql(context.Request.Params["items"]);
                items = items.Replace("，", ",");
                itemList = JsonConvert.DeserializeObject<List<Model.VoteItem>>(items);

                string selOffice = CommonFunction.VarToStr(context.Request.Params["selOffice"]);
                if (selOffice != "")
                    subOffices = JsonConvert.DeserializeObject<List<Model.VoteSubOffice>>(selOffice);
            }
            catch (JsonReaderException)
            {
            }

            //
            Model.JsonResponse res = new Model.JsonResponse();
            if (model.Id == 0)
            {
                if (bll.Add(model, itemList, subOffices) > 0)
                {
                    res.success = true;
                }
                else
                {
                    res.success = false;
                    res.Message = bll.errorString;
                }
            }

            return JsonConvert.SerializeObject(res);
        }

        /// <summary>
        /// 删除一条投票
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        private string DeleteData(HttpContext context)
        {
            string ids = context.Request["id"];
            //提交数据
            Model.JsonResponse rsp = new Model.JsonResponse();
            if (bll.DeleteList(ids))
            {
                rsp.success = true;
            }
            else
            {
                rsp.success = false;
                rsp.Message = bll.errorString;
            }
            return JsonConvert.SerializeObject(rsp);
        }
        private void GetSubOfficeJosnForGrid(HttpContext context)
        {
            BLL.SubOffice bll = new BLL.SubOffice();
            context.Response.Write(bll.GetSubOfficeJosnForGrid());
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
